//
//  XLTableViewMainController.m
//  Current
//
//  Created by 于鹏 on 2017/8/16.
//  Copyright © 2017年 YIVIEW. All rights reserved.
//

#import "XLTableViewMainController.h"
#import "NSArray+XLSafeAccess.h"
//#import <BaseModule/NSArray+XLSafeAccess.h>
//分组section之间的距离
#define XL_SectonHeight 10
@interface XLTableViewMainController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation XLTableViewMainController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self registerNib];
}

#pragma mark - Override
- (void)initInterface {
    [self.view addSubview:self.tableView];
    [self setupTableViewConstraint];
}


//设置tableView约束
- (void)setupTableViewConstraint {
    
    NSLayoutConstraint *tableViewTopConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    [self.view addConstraint:tableViewTopConstraint];
    
    NSLayoutConstraint *tableViewLeftConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
    [self.view addConstraint:tableViewLeftConstraint];
    
    NSLayoutConstraint *tableViewWidthConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
    [self.view addConstraint:tableViewWidthConstraint];
    
    NSLayoutConstraint *tableViewHeightConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
    [self.view addConstraint:tableViewHeightConstraint];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.xl_dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSMutableArray *rows = [self.xl_dataSource xl_objectWithIndex:section];
    return rows.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *sections = [self.xl_dataSource xl_objectWithIndex:indexPath.section];
    XLBaseCellModel *cellModel = (XLBaseCellModel *)[sections xl_objectWithIndex:indexPath.row];
    Class class = NSClassFromString(cellModel.cellClass);
    XLBaseTableViewCell *cell = [class cellWithIdentifier:cellModel.cellClass tableView:tableView];
    if (self.mainDelegate && [self.mainDelegate respondsToSelector:@selector(xl_mainTableView:mainTableViewCell:mainCellModel:sectionArr:atIndexPath:)]) {
        [self.mainDelegate xl_mainTableView:tableView mainTableViewCell:cell mainCellModel:cellModel sectionArr:sections.copy atIndexPath:indexPath];
    }
    
    [cell setupDataModel:cellModel];
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *sections = [self.xl_dataSource xl_objectWithIndex:indexPath.section];
    XLBaseCellModel *cellModel = (XLBaseCellModel *)[sections xl_objectWithIndex:indexPath.row];
    if (self.mainDelegate && [self.mainDelegate respondsToSelector:@selector(xl_mainTableView:didSelectRowAtIndexPath:cellModel:)]) {
        [self.mainDelegate xl_mainTableView:tableView didSelectRowAtIndexPath:indexPath cellModel:cellModel];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *sections = [self.xl_dataSource xl_objectWithIndex:indexPath.section];
    XLBaseCellModel *cellModel = (XLBaseCellModel *)[sections xl_objectWithIndex:indexPath.row];
    Class class = NSClassFromString(cellModel.cellClass);
    if (self.mainDelegate && [self.mainDelegate respondsToSelector:@selector(xl_mainTableView:heightForRowAtIndexPath:)]) {
        CGFloat height = [self.mainDelegate xl_mainTableView:tableView heightForRowAtIndexPath:indexPath];
        if (height > 0) {
            return height;
        } else {
            return [class getCellHeight:cellModel];
        }
    } else {
        return [class getCellHeight:cellModel];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    //如果是最后一个section
    if (section == self.xl_dataSource.count - 1) {
        return 0;
    }
    return XL_SectonHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    //如果是最后一个section
    if (section == self.xl_dataSource.count - 1) {
        return nil;
    }
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, XL_SectonHeight)];
    [view setBackgroundColor:[UIColor clearColor]];
    return view;
}
#pragma mark - Public Method

- (void)registerNib {
    
}

//tableview header
- (UIView *)defaultTableHeader
{
        UIView *defaultHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 10)];
        [defaultHeaderView setBackgroundColor:kXLClearColor];
        return defaultHeaderView;
}



#pragma mark - Getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = kBackground_color;
        _tableView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _tableView;
}

- (NSMutableArray *)xl_dataSource {
    if (!_xl_dataSource) {
        _xl_dataSource = [NSMutableArray array];
    }
    return _xl_dataSource;
}

#pragma mark - Setter
- (void)setTableHeaderView:(UIView *)tableHeaderView {
    _tableHeaderView = tableHeaderView;
    self.tableView.tableHeaderView = tableHeaderView;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
